/*
 * Copyright 2008 Zoundry LLC, Inc.  All Rights Reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 */
package org.zoundry.gaedt.core.models.application;

import java.util.Map;
import org.zoundry.gaedt.core.util.ZGaeUtil;

/**
 * Factory used to create instances of application handlers from data
 * in the app.yaml file.
 */
public class ZGaeHandlerModelFactory
{
    /**
     * Creates a handler from the given yaml data.
     *
     * @param aHandlerData
     */
    public static IZGaeHandlerModel createHandler(Map<String, Object> aHandlerData)
    {
        String script = (String) aHandlerData.get("script");
        String staticDir = (String) aHandlerData.get("static_dir");
        String staticFiles = (String) aHandlerData.get("static_files");
        if (script != null)
            return createScriptHandler(aHandlerData);
        if (staticDir != null)
            return createStaticDirectoryHandler(aHandlerData);
        if (staticFiles != null)
            return createStaticFilePatternHandler(aHandlerData);
        return null;
    }

    /**
     * Creates a script handler.
     *
     * @param aHandlerData
     */
    private static IZGaeHandlerModel createScriptHandler(Map<String, Object> aHandlerData)
    {
        String url = (String) aHandlerData.get("url");
        String script = ZGaeUtil.getSafeString( (String) aHandlerData.get("script") );
        String login = ZGaeUtil.getSafeString( (String) aHandlerData.get("login") );

        TZGaeHandlerLoginType loginType = TZGaeHandlerLoginType.None;
        if ("required".equals(login))
            loginType = TZGaeHandlerLoginType.Required;
        else if ("admin".equals(login))
            loginType = TZGaeHandlerLoginType.Admin;

        ZGaeScriptHandlerModel handlerModel = new ZGaeScriptHandlerModel();
        handlerModel.setUrl(url);
        handlerModel.setScript(script);
        handlerModel.setLogin(loginType);
        return handlerModel;
    }

    /**
     * Creates a static directory handler.
     *
     * @param aHandlerData
     */
    private static IZGaeHandlerModel createStaticDirectoryHandler(Map<String, Object> aHandlerData)
    {
        String url = ZGaeUtil.getSafeString( (String) aHandlerData.get("url") );
        String staticDir = ZGaeUtil.getSafeString( (String) aHandlerData.get("static_dir") );
        String login = ZGaeUtil.getSafeString( (String) aHandlerData.get("login") );
        String expiration = ZGaeUtil.getSafeString( (String) aHandlerData.get("expiration") );
        String mimeType = ZGaeUtil.getSafeString( (String) aHandlerData.get("mime_type") );

        TZGaeHandlerLoginType loginType = TZGaeHandlerLoginType.None;
        if ("required".equals(login))
            loginType = TZGaeHandlerLoginType.Required;
        else if ("admin".equals(login))
            loginType = TZGaeHandlerLoginType.Admin;

        ZGaeStaticDirectoryHandlerModel handlerModel = new ZGaeStaticDirectoryHandlerModel();
        handlerModel.setUrl(url);
        handlerModel.setStaticDir(staticDir);
        handlerModel.setLogin(loginType);
        handlerModel.setExpiration(expiration);
        handlerModel.setMimeType(mimeType);
        return handlerModel;
    }

    /**
     * Creates a static file pattern handler.
     *
     * @param aHandlerData
     */
    private static IZGaeHandlerModel createStaticFilePatternHandler(Map<String, Object> aHandlerData)
    {
        String url = ZGaeUtil.getSafeString( (String) aHandlerData.get("url") );
        String staticFiles = ZGaeUtil.getSafeString( (String) aHandlerData.get("static_files") );
        String login = ZGaeUtil.getSafeString( (String) aHandlerData.get("login") );
        String expiration = ZGaeUtil.getSafeString( (String) aHandlerData.get("expiration") );
        String mimeType = ZGaeUtil.getSafeString( (String) aHandlerData.get("mime_type") );
        String upload = ZGaeUtil.getSafeString( (String) aHandlerData.get("upload") );

        TZGaeHandlerLoginType loginType = TZGaeHandlerLoginType.None;
        if ("required".equals(login))
            loginType = TZGaeHandlerLoginType.Required;
        else if ("admin".equals(login))
            loginType = TZGaeHandlerLoginType.Admin;

        ZGaeStaticFilePatternHandlerModel handlerModel = new ZGaeStaticFilePatternHandlerModel();
        handlerModel.setUrl(url);
        handlerModel.setStaticFiles(staticFiles);
        handlerModel.setLogin(loginType);
        handlerModel.setExpiration(expiration);
        handlerModel.setMimeType(mimeType);
        handlerModel.setUpload(upload);
        return handlerModel;
    }
}
